package com.xiaoke.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoke.domain.ResponseResult;
import com.xiaoke.domain.dto.CreateCategoryDTO;
import com.xiaoke.domain.dto.PagingDTO;
import com.xiaoke.domain.entity.Article;
import com.xiaoke.domain.entity.Category;
import com.xiaoke.domain.vo.AdminGetCategoryListVo;
import com.xiaoke.domain.vo.AdminGetCategoryVo;
import com.xiaoke.domain.vo.PageVo;
import com.xiaoke.exception.QueryException;
import com.xiaoke.mapper.CategoryMapper;
import com.xiaoke.service.AdminArticleService;
import com.xiaoke.service.AdminCategoryService;
import com.xiaoke.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xiaoke.enums.AppHttpCodeEnum.*;

/**
 * 文章分类表(Category)表后台服务实现类
 *
 * @author xiaoke
 * @since 2023-03-17 10:25:48
 */
@Service("adminCategoryService")
public class AdminCategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements AdminCategoryService {

    @Resource
    AdminArticleService adminArticleService;
    
    @Override
    public ResponseResult getCategoryList(PagingDTO dto) {
        Page<Category> categoryPagePage = new Page<>(dto.getPageNum(),dto.getPageSize());

        Page<Category> page = page(categoryPagePage);

        List<AdminGetCategoryListVo> list = BeanCopyUtils.copyBeanList(page.getRecords(), AdminGetCategoryListVo.class);

        list.stream()
                .peek(category ->{
                    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(Article::getCategoryid,category.getId());
                    category.setCount(adminArticleService.count(queryWrapper));
                }).collect(Collectors.toList());

        return ResponseResult.okResult(new PageVo(list,page.getTotal()));
    }

    @Override
    public ResponseResult getCategory(Long id) {
        Category category = getById(id);
        if(category == null) {
            throw  new QueryException();
        }
        AdminGetCategoryVo  categoryVo = BeanCopyUtils.copyBean(category, AdminGetCategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @Override
    public ResponseResult CreateCategoryDTO(CreateCategoryDTO dto) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,dto.getName());

        if(count(queryWrapper) != 0) {
            return ResponseResult.errorResult(CREATE_ERROR);
        }
        Category category = category(dto);
        save(category);
        return ResponseResult.okResult(category.getId(),CREATE_SUCCESS);
    }

    @Override
    public ResponseResult updateCategory(CreateCategoryDTO dto, Long id) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Category::getId,id);

        Category category = getOne(queryWrapper);

        if(category == null) {
            throw new QueryException();
        }

        Category Newcategory = category(dto);

        BeanCopyUtils.copyNonNullProperties(Newcategory,category);

        update(category,queryWrapper);

        return ResponseResult.okResult(MODIFY_SUCCESS);
    }

    @Override
    public ResponseResult delCategory(Long id) {
        boolean del = removeById(id);
        if(!del) {
            return ResponseResult.errorResult(DEL_ERROR);
        }
        return ResponseResult.okResult();
    }
    /**
     * 分类实体赋值
     */
    public Category category(CreateCategoryDTO dto) {
        Category category = new Category();
        category.setName(dto.getName());
        category.setStatus(String.valueOf(dto.getStatus()));
        return  category;
    }
}
